<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script src="jquery-1.9.1.js"></script>
<style>
    input{
        border: 1px solid darkred;
    }
    #last{

        margin-left: 16px;
    }
    #admin_span{
        width: 100px;
        height: 10px;

    }
    #password_span{
        width: 100px;
        height: 10px;

    }
    .show{

    }
</style>
<body>
<!--http://localhost:8080/03_servlet/formservlet-->
<!--action="http://localhost:8080/03_servlet/formservlet" method="get"-->
<form action="http://localhost:8080/03_servlet/servlet1" method="get">
    <label>用户名:</label>
    <input type="text" name="admin" placeholder="请输入10位数字"   >
    <span id="admin_span"></span>
    <br>
    <label>密码:  </label>
    <input id="last" type="password" name="password"  placeholder="五位字母，五位数字">
    <span id="password_span"></span>
    <br>
    <!--     兴趣爱好-->
    <label>兴趣爱好:</label><br>
    java<input type="checkbox"  name="hobby" value="java">
    javascript<input type="checkbox" name="hobby" value="javascript" >
    c++<input type="checkbox" name="hobby" value="c++" >
    <input type="submit"/>
</form>
<script>
    let admin=document.querySelector('[name="admin"]');
    let password=document.querySelector('[name="password"]');
    let checkboxs=document.querySelectorAll('[name="hobby"]');
    let submit=document.querySelector('[type="submit"]');



    admin.addEventListener("change",function () {
        if(!(/^\d{10}$/g.test($(this).val()))){
            $("#admin_span").css("background-color","#E74C3C");
            $("#admin_span").text("您的格式有问题");

        }

        else{
            $("#admin_span").css("background-color","#2ECC71");
            $("#admin_span").text("✔");
            setInterval(() => {
                $("#admin_span").css("background-color","white");

            }, 1000);

        }
    });
    password.addEventListener("change",function () {
        if(!(/\w{10}/g.test($(this).val()))){
            $("#password_span").css("background-color","#E74C3C");
            $("#password_span").text("您的格式有问题");


        }
        else{
            $("#password_span").css("background-color","#2ECC71");
            $("#password_span").text("✔");
            setInterval(() => {
                $("#password_span").css("background-color","white");

            }, 2000);

        }
    });
admin.addEventListener("blur",function () {
 if($('[name="admin"]').val()==""){
        $("#admin_span").text("账号不能为空哦!");
        $("#admin_span").css("background-color","#E74C3C");
    }
})
    password.addEventListener("blur",function () {
        if($('[name="password"]').val()==""){
            $("#password_span").text("密码不能为空哦!");
            $("#password_span").css("background-color","#E74C3C");
        }
    })

    let boxs=$("[type='checkbox']");

    boxs.click(function(){
        $(this).toggleClass("show");
    })

    submit.addEventListener("click",function (event) {
        if(getchecked(checkboxs)==3) {
            event.preventDefault();//阻止默认行为
            event.defaultPrevented
            alert("爱好不能为空!")
        }

        else{
            alert("提交成功")
        }
    })
    function getchecked(checkboxs) {
        let count=0;
        for (const box of checkboxs) {
            if(!($(box).hasClass("show"))) {
                count++;
            }
        }
        return count;
    }
    function getadmin() {
        if ($('[name="admin"]').val()==null ){
            return false;
        }
        return  true;
    }

</script>
</body>
</html>